<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetCart.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\cart;

use app\api\model\dao\Ad;
use app\api\model\dao\AdPosition;
use app\api\model\dao\Cart;
use app\api\model\dao\Goods;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetCart extends PageBase
{

    private $objCart;
    private $objGoods;
    private $objAd;
    private $objAdPosition;

    public function __construct()
    {
        parent::__construct();
        $this->objCart = new Cart();
        $this->objGoods = new Goods();
        $this->objAd = new Ad();
        $this->objAdPosition = new AdPosition();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
        ];
        $msg = [
            'user_id.require' => '用户ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        //if (false === $ret) {
        //    return $this->getRes(Error::ERR_NO_AUTH);
        //}

        // 获取订单信息
        $arrCond = array();
        $arrCond['user_id'] = !empty($this->user_id) ? $this->user_id : 0;

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $counts = $this->objCart->getCount($arrCond);
        $rets = $this->objCart->getCartByCond($arrCond, [], $offset, $limit, ' goods_id desc ');

        if ($rets === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        // 获取商品信息
        $goods_arr = [];
        $goods_ids = array_map(function ($v) {
            return $v['goods_id'];
        }, $rets);
        $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
        foreach ($goods as $good) {
            $goods_arr[$good['goods_id']] = $good;
        }

        foreach ($rets as &$v) {
            if (isset($goods_arr[$v['goods_id']])) {
                // 加上折扣
                $v['discount'] = '' . round($goods_arr[$v['goods_id']]['shop_price'] / $goods_arr[$v['goods_id']]['market_price'], 2) * 10;
                $v['goods_thumb'] = $goods_arr[$v['goods_id']]['goods_thumb'];
            }
        }

        $now = time();
        // 获取购物车广告
        $arrFields = [
            'ad_id',
            'ad_name',
            'ad_link',
            'ad_code',
            'ad_type',
            'ad_value',
        ];
        $arrConds = [
            'pid' => ['=', 2],
            'enabled' => ['=', 1],
            'status' => ['=', '0'],
            //'start_time' => ['<=', $now],
            //'end_time' => ['>=', $now],
        ];
        $ads = $this->objAd->getAdByCond($arrConds, $arrFields);
        $ads = !empty($ads) ? $ads : array();

        // 推荐商品
        $arrFields = [
            'goods_id',
            'goods_name',
            'shop_price',
            'market_price',
            'goods_thumb',
            'sale_count',
        ];
        $goods = $this->objGoods->getGoodsByCond([], $arrFields, 0, 3);
        if (!empty($goods)) {
            foreach ($goods as $good) {
                $good['discount'] = '' . round($good['shop_price'] / $good['market_price'], 2) * 10;
            }
        }

        $this->data = array(
            'list' => $rets,
            'total' => $counts,
            'ad' => $ads,
            'recommend_goods' => $goods,
        );

        return $this->getRes();
    }
}
